﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    ///钉钉TMS确认数量推送
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DIDI_TMSShipQtyController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public DIDI_TMSShipQtyController(ILogger<DIDI_TMSShipQty_tmsRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            _logger = loggerFactory.CreateLogger<DIDI_TMSShipQtyController>();

        }

        /// <summary>
        ///  钉钉TMS确认数量推送
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostAddDIDI_TMSShipQty([FromBody] DIDI_TMSShipQty_tmsRequestDto strparm)
        {
            string strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();

            DIDI_TMSShipQty_TMSResponseDto re = new DIDI_TMSShipQty_TMSResponseDto();
            try
            {
                Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
                dic.Add("date", "");
                DataTable dt_qty = MethodHelp.GetTableInfo("TMS0019", dic, con);
                if (dt_qty.Rows.Count > 0)
                {
                    foreach (DataRow item in dt_qty.Rows)
                    {
                        DIDI_TMSShipQtyRequestDto rq = new DIDI_TMSShipQtyRequestDto();
                        rq.date = DB.ToDateTime(item["ddate"].ToString()).ToString("yyyyMMdd");
                        rq.tmsConfirm = DB.ToDouble(item["tms_amount"].ToString());
                        rq.source = "TMS";

                        Dictionary<string, string> urldic = new Dictionary<string, string>(); //查询公用dic
                        //urldic.Add("function_description", "钉钉_物流确认数量推送");//查看URL配置表地址描述
                        //DataTable url_dt = MethodHelp.GetTableInfo("TMS0004", urldic, con);//获取URL地址
                        //if (url_dt.Rows.Count > 0)
                        //{

                        //    string jsonText = JsonConvert.SerializeObject(rq);
                        //    var reAgv = DB.httpPost(url_dt.Rows[0]["interface_url"].ToString(), jsonText, "", "否");
                        //    DIDI_TMSShipQtyResponseDto psp = JsonConvert.DeserializeObject<DIDI_TMSShipQtyResponseDto>(reAgv);
                        //    if (psp.status != 200)
                        //    {
                        //        strMsg = strMsg+"；DIDI ERR:" + psp.message;
                        //    }

                        //}

                        urldic = new Dictionary<string, string>(); //查询公用dic
                        urldic.Add("function_description", "钉钉_物流确认数量推送_高景");//查看URL配置表地址描述
                        DataTable url_dt2 = MethodHelp.GetTableInfo("TMS0004", urldic, con);//获取URL地址
                        if (url_dt2.Rows.Count > 0)
                        {

                            string jsonText = JsonConvert.SerializeObject(rq);
                            var reAgv2 = DB.httpPost(url_dt2.Rows[0]["interface_url"].ToString(), jsonText, "", "否");
                            DIDI_TMSShipQty_GKResponseDto psp2 = JsonConvert.DeserializeObject<DIDI_TMSShipQty_GKResponseDto>(reAgv2);
                            if (psp2.code != 200)
                            {
                                strMsg = strMsg + "；DIDI ERR:" + psp2.msg;
                            }

                        }
                    }
                }

            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

            if (strMsg == "")
            {
                re.code = 0;
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }



            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);

        }


    }
}
